package vn.hut.soict.cs.phanquanghuy.neslegend.emulator.util;

public class NumberUtil {
	public final static int BYTE_MASK = 0x000000ff;
	public final static int WORD_MASK = 0x0000ffff;
	
	public final static int BIT0 = 0b00000001;
	public final static int BIT1 = 0b00000010;
	public final static int BIT2 = 0b00000100;
	public final static int BIT3 = 0b00001000;
	public final static int BIT4 = 0b00010000;
	public final static int BIT5 = 0b00100000;
	public final static int BIT6 = 0b01000000;
	public final static int BIT7 = 0b10000000;
	
	
	public final static int[] BIT_MASK_ARRAY = new int[] {
		0x00000001,
		0x00000002,
		0x00000004,
		0x00000008,
		0x00000010,
		0x00000020,
		0x00000040,
		0x00000080
	};
	
	
	/**
	 * Make byte from integer (because java not support unsigned type)
	 * @param value
	 * @return
	 */
	public final static int roundByte(int value) {
		return value & BYTE_MASK;
	}
	
	/**
	 * Make word from integer
	 * @param value
	 * @return
	 */
	public final static int roundWord(int value) {
		return value & WORD_MASK;
	}
	
	/**
	 * Make word from 2 byte. Ex: l = 0x1f; h = 0x2c; w = 0x2c1f  
	 * @param lowByte
	 * @param highByte
	 * @return
	 */
	public final static int makeWord(int lowByte, int highByte) {
		int word = roundByte(highByte);
		word = word << 8;
		word += roundByte(lowByte);
		
		return word;
	}
	
	//Memory Page Size of 6502 is 256 byte
	
	public final static boolean checkCrossedPage(int address1, int address2) {
		int pageIndex1 = address1 >> 8;
		int pageIndex2 = address2 >> 8;
		
		return pageIndex1 == pageIndex2;
	}
	
	public final static boolean getBit(int number, int bit) {
		number = roundByte(number);
		return (number & BIT_MASK_ARRAY[bit % 8]) != 0;
	}
}
